package com.gsk.europepharma.salesReport.struts.actions;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class ExportGridDoctorsPdfUga extends Action {
	
	private static final int DEFAULT_BUFFER_SIZE = 10240; 
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Parametres 
		String codeUga = request.getParameter("codeUga");
		
		String cs_division_string = request.getParameter("cs_division");
		Integer cs_division = Integer.parseInt(cs_division_string);
		String periodeActivite = request.getParameter("periodeActivite");
		String specialite = request.getParameter("specialite");
		String segments = request.getParameter("segments");
		
		Properties prop = new Properties();
		prop.load(Parametrage.class.getClassLoader().getResourceAsStream("config.properties"));
		
		File temp = new File(prop.getProperty("dir") + "docteurs.pdf");
		Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(temp));
        document.open();
        
        // Style
        
        BaseColor colorHeader = new BaseColor(122, 156, 209);
        
        BaseColor colorLinePeer = new BaseColor(192, 192, 192);
        
        Font fontHeader = new Font();
        fontHeader.setColor(BaseColor.WHITE);
        fontHeader.setSize(9);
        fontHeader.setStyle(Font.BOLD);
        
        Font fontLine = new Font();
        fontLine.setSize(8);
        
        PdfPCell cell;
        
        PdfPTable table = new PdfPTable(5);
        table.setWidths(new int[] {2, 2, 1, 3, 1});
        
        cell = new PdfPCell(new Phrase("Nom", fontHeader));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(colorHeader);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Prénom", fontHeader));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(colorHeader);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Code postal", fontHeader));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(colorHeader);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Ville", fontHeader));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(colorHeader);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("Spécialité", fontHeader));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(colorHeader);
        table.addCell(cell);
        
        // Requete SQL
        tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
		Session session = salesDAO.getSession();
					
		Query req = null;
		
		String strReq = salesDAO.QUERY_QUERY_LISTE_CONTACT_UGA;
		
		req = session.getNamedQuery(strReq); 
		req.setString("uga", codeUga);
		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		
        BaseColor colorLine = colorLinePeer;
        
        int peer = 0;
		
		while (iter.hasNext()) {
			Object[] val = (Object[]) iter.next();
			
			if ((peer % 2) == 0) {
        		colorLine = colorLinePeer;
        	} else {
        		colorLine = BaseColor.WHITE;
        	}
        	peer++;

        	cell = new PdfPCell(new Phrase(val[4].toString(), fontLine));
        	cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        	cell.setBackgroundColor(colorLine);
        	table.addCell(cell);
        	
        	cell = new PdfPCell(new Phrase(val[5].toString(), fontLine));
        	cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        	cell.setBackgroundColor(colorLine);
        	table.addCell(cell);
        	
        	cell = new PdfPCell(new Phrase(val[7].toString(), fontLine));
        	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        	cell.setBackgroundColor(colorLine);
        	table.addCell(cell);
        	
        	cell = new PdfPCell(new Phrase(val[8].toString(), fontLine));
        	cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        	cell.setBackgroundColor(colorLine);
        	table.addCell(cell);
        	
        	cell = new PdfPCell(new Phrase(val[9].toString(), fontLine));
        	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        	cell.setBackgroundColor(colorLine);
        	table.addCell(cell);
		}
		
        document.add(table);
        
        document.close();
        
		BufferedInputStream input = null;
        BufferedOutputStream output = null;
        // Ouverture du fichier 
        input = new BufferedInputStream(new FileInputStream(temp), DEFAULT_BUFFER_SIZE);
        // Initialisation de la reponse
        response.reset();
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-Length", String.valueOf(temp.length()));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + "docteurs.pdf" + "\"");
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
        // Ecriture dans la réponse
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        input.close();
        response.getOutputStream().flush();

		return null;
	}
	
}
